<?php

require_once 'Auth/OpenID/Consumer.php';
require_once 'Auth/OpenID/FileStore.php';
//require_once 'PipesImpl/User/Auth.php';

class Pipes_User_Auth_OpenID //implements PipesImpl_User_Auth
{
	private $_openid_url = '';
	private $_consumer = null;
	private $_response = null;

	public function __construct(array $returnValues = array()) {
		$path = '/tmp/_pipes/openid';
		$this->_consumer = new Auth_OpenId_Consumer(new Auth_OpenId_FileStore($path));

		if (count($returnValues) > 0) {
			$this->_response = $this->_consumer->complete($returnValues);
		}
	}

	public function process($identity_url, $return, $trust) {
		$request = $this->_consumer->begin($identity_url);
		$request->addExtensionArg('sreg', 'optional', 'email');
		$redirectUrl = $request->redirectURL($trust, $return);
		
		// make sure the session is closed prior to redirect
		session_write_close();
		header('Location: ' . $redirectUrl);
		exit;

	}

	public function __get($key) {
		switch ($key) {
			case 'status' :
			case 'identity_url':
				return $this->_response->$key;
		}
	}

	public function isValid() {
		return ($this->status == Auth_OpenID_SUCCESS);
	}
}

